/*
 * Copyright 2010 Christopher Z. Collier
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.xorf.caveman.gui;

import java.awt.Component;
import java.awt.FlowLayout;

import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;

public class StringArrayCellRenderer extends JPanel implements ListCellRenderer {
    JLabel[] labels;
    int numCols;
    
    public StringArrayCellRenderer(int numCols) {
        setLayout(new FlowLayout(FlowLayout.LEFT));
        this.numCols = numCols;            
        labels = new JLabel[numCols];
        
        for (int i = 0; i < numCols; i++) {
            JLabel label = new JLabel();
            labels[i] = label;
            label.setOpaque(true);
            add(label);
        }
    }
    
    //@Override
    public Component getListCellRendererComponent(JList list, Object value,
            int index, boolean isSelected, boolean cellHasFocus) {
        
        String[] dataRow = (String[])value;
        
        for (int i = 0; i < numCols; i++) {
            labels[i].setText(dataRow[i]);                
        }
        
        setEnabled(list.isEnabled());
        setFont(list.getFont());
        return this;
    }
}

